<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td colspan=2 nowrap>
	<fieldset><legend align="top"><{t}>公式验算<{/t}></legend>
        <div class='notice'> 您可以在这里测试配送公式是否计算正确、有效 </div>
        <table width="98%" border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td width="70%" nowrap>配送公式：
              <input type="text" id="dlg_expressions" value="<{$expressions}>" size="50" /></td>
          </tr>
          <tr>
            <td width="70%" nowrap>商品重量：
              <input type="text" id="weight" name="weight" class="inputstyle" maxlength="20" size="8" value="0">
              克 </td>
            <td width="30%" nowrap rowspan="3">
			  <p><button id="btn-calculate" class='sysiconBtnNoIcon'><{t}>计算<{/t}></button>
              </p></td>
          </tr>
          <tr>
            <td width="70%" nowrap>订单价格：
              <{input type="money" id="orderprice" name="orderprice" class="inputstyle" maxlength="20" size="8" value="0"}> </td>
          </tr>
          <tr>
            <td width="70%" nowrap>
			  <span id="result"></span>
		    </td>
          </tr>
        </table>
      </fieldset></td>
  </tr>
</table>
<div class='mainFoot' style='height:40px;width:80px;margin:0 auto;'>
  <div class="table-action"> <{button type="button" id="btn-confirm" label="确定"}> </div>
</div>
<script language="javascript">
(function() {
    $('btn-calculate').addEvent('click',function(){countexp();});
    $('btn-confirm').addEvent('click',function(){countexp(this)});
    function getval(expval) {
		if (eval(expval) > 0.000001) {
			return 1;
		} else if (eval(expval) > - 0.000001 && eval(expval) < 0.000001) {
			return 1 / 2;
		} else {
			return 0;
		}
	}
	function getceil(expval) {
		if (eval(expval) > 0) {
			return Math.ceil(eval(expval) - 0.000001);
		} else {
			return 0;
		}
	}
	function countexp(qd) {
		var bds = $('dlg_expressions').value;
		if (!qd) {
			if (bds == '') {
				$('dlg_expressions').focus();
				alert("请输入配送公式");
				return;
			}
		}
		var re = new RegExp("/^[^\]\[\}\{\)\(0-9WwPp\+\-\/\*]+$/");
		if (re.test(bds)) {
			alert("公式中含有非法字符");
			$('dlg_expressions').focus();
			return;
		}
		var price = $('orderprice').value;
		var weight = $('weight').value;
		var str;
		str = bds.replace(/(\[)/g, "getceil(");
		str = str.replace(/(\])/g, ")");
		str = str.replace(/(\{)/g, "getval(");
		str = str.replace(/(\})/g, ")");
		str = str.replace(/(W)/g, weight);
		str = str.replace(/(w)/g, weight);
		str = str.replace(/(P)/g, price);
		str = str.replace(/(p)/g, price);
		try {
			eval(str);
		}
		catch(e) {
			alert("公式格式不正确");
			return;
		}
		$('result').set('html', '<b><{t}>计算结果：' + Math.floor(eval(str) * 100 + 0.01) / 100 + '<{/t}></b>');
		if (!qd) return;
		qd = $(qd);
		var dialog = qd.getParent(".dialog");
		$E('[name=dt_expressions]').set("value", $("dlg_expressions").getValue());
		dialog.retrieve("instance").close();
	}
})()
</script>
